﻿
package com.ostrich.manpower.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.StringUtil;
import com.ostrich.manpower.bean.TostHrAccfundinfo;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.TostHrAccfundinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrAccfundinfoService")
public class TostHrAccfundinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrAccfundinfoService.class);

	@Autowired
    private TostHrAccfundinfoDao<T> dao;

	@Autowired
	private TostHrBaseinfoService tosthrbaseinfoservice;
	
	public TostHrAccfundinfoDao<T> getDao() {
		return dao;
	}
	public List<T> queryAccfund(int hrid){
		return dao.queryAccfund(hrid);
	}
	
	
	
	public List<T> queryAccfunds(@Param("hmeMonth")String hmeMonth,@Param("hrId")int hrId){
		return dao.queryAccfunds(hmeMonth,hrId);
	}
	

	/****
	 * 判断月份是否存在
	 * @param hmeMonth
	 * @return
	 */
	private boolean isExistByHmemonth(String hacMonth) {
		int coun= getDao().isExistByHmemonth(hacMonth);
		return coun>0?true:false;
	}
	
     /****
	 * 根据sheet插入数据库
	 * List<TostHrAccfundinfo>
	 * @param sheet
	 * @return
	 */
	public String setAccfundinfoBySheet(HSSFSheet sheet,String month)throws Exception {
		//List<TostHrAccfundinfo> infos = new ArrayList<TostHrAccfundinfo>();
		HSSFRow row = null;
		int colnum = 11;
		String msg = "";
		//判断该月是否存在，存在给予提示切不导入
		// if(isExistByHmemonth(month)){
		// msg="数据库已经导入过该月数据，如果需要重新导入请删除该月历史数据。";
		// return msg;
		// }
		FormulaUtil.refresh();
		// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 除去标题跟表头
		for (int j = 2; sheet!=null && j < sheet.getPhysicalNumberOfRows(); j++) {
			row = sheet.getRow(j);
			TostHrAccfundinfo tmeal = new TostHrAccfundinfo();
			String iscontinut = (row!=null)?ImportUtil.getCellValue(row.getCell(1)):null;
       	 	TostHrBaseinfo pid= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(iscontinut);
       	 	pid = (pid==null)?new TostHrBaseinfo():pid;
       	 	if(pid==null|| pid.getId()==null){
       	 		msg +=(pid!=null&&pid.getId()!=null)?"":"第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的公积金没有正常导入（没有该工号）。";
       	 		continue;
       	 	}
       	 	if(iscontinut!=null && iscontinut.length()>0 && (ImportUtil.getCellValue(row.getCell(2))).equalsIgnoreCase(pid.getHrName())){
			  for (int i = 0; i < colnum; i++) {
				String str = ImportUtil.getCellValue(row.getCell(i));
					if(i==1)
					{
						if(pid!=null && pid.getId()!=null)
						tmeal.setHrId(pid.getId());
					}
				else if (i == 2)
					if(str!=""&&str!=null)
						tmeal.setHacName(str);
						else {
							tmeal.setHacName(pid.getHrName());
						}
				else if (i == 3) {
					if(str!=""&&str!=null)
						tmeal.setHacSex(str);
						else {
							tmeal.setHacSex("");
						}
					
				} else if (i == 4) {
					if(str!=""&&str!=null)
						tmeal.setHacAccountno(str);
						else {
							tmeal.setHacAccountno("此处未填写数据");
						}
					
				} else if (i == 5) {
					if(str!=""&&str!=null)
						tmeal.setHacType(str);
						else {
							tmeal.setHacType("");
						}
					
				} else if (i == 6) {
					if(str!=""&&str!=null)
					tmeal.setHacIdnmuber(str);
					else {
						tmeal.setHacIdnmuber("");
					}
				} else if (i == 7) {
					Float flstr = (str != null && str.trim().length() > 0 && StringUtil
							.isNumeric(str)) ? Float.valueOf(str)
							: new Float(0);
							System.out.print(StringUtil.isNumeric(str));
					tmeal.setHacDebase(flstr);
				} else if (i == 8) {
					Float flstr = (str != null && str.trim().length() > 0 && StringUtil
							.isNumeric(str)) ? Float.valueOf(str)
							: new Float(0);
					tmeal.setHacFactunitpay(flstr);
				} else if (i == 9) {
					Float flstr = (str != null && str.trim().length() > 0 && StringUtil
							.isNumeric(str)) ? Float.valueOf(str)
							: new Float(0);
					tmeal.setHacFactpersonpay(flstr);
					tmeal.setHacFactamount(tmeal.getHacFactpersonpay()+tmeal.getHacFactunitpay());
				} else if (i == 10) {
					str=(str!=null&&str.indexOf(".")>0)?str.substring(0,str.indexOf(".")):str;
					tmeal.setHacStatus(str);
					tmeal.setHacMonth(month);
				}
			  }
       	 	}else{
       	 		msg +="第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的公积金没有正常导入（工号跟姓名不匹配）。";
       	 		continue;
       	 	}
       	 
			if (tmeal != null && tmeal.getHrId() != null) {
				//项目代建费用汇总表的算法
				Map<String,Object> fMap = FormulaUtil.formulaById(6, tmeal.getHacDebase());
				if(fMap!=null){
				tmeal.setHacUnitpay((Float)fMap.get("comp"));
				tmeal.setHacPersonpay((Float)fMap.get("person"));
				tmeal.setHacAmount((Float)fMap.get("totle"));
				}
				//根据员工编号查询是否有该员工
	        	 List<TostHrAccfundinfo> bslg= (List<TostHrAccfundinfo>) queryAccfunds(month,tmeal.getHrId());
				if(bslg!=null&&bslg.size()>0){
					TostHrAccfundinfo acf = bslg.get(0);
					if(acf!=null && acf.getId()!=null){
						tmeal.setId(acf.getId());
						dao.update((T)tmeal);
					}
				}else{
					dao.add((T) tmeal);
				}
				//infos.add(tmeal);
			}
		}
		return msg;
	}
	public List<T> queryAccfundByDerpart(TostHrAccfundinfo tosthraccfundinfo,String derpart,String startM,String endM){
		return getDao().queryAccfundByDerpart(tosthraccfundinfo, derpart,startM, endM);
		
	}
/*
	public String setMealCellVal(String filepath,String month) throws Exception {
		String msg="";
		InputStream fis = new FileInputStream(filepath);
		HSSFSheet sh = ImportUtil.getSheet(fis);
	    msg=getMealBySheet(sh,month);// 写数据库的方法
		return msg;
	}*/
}
